/*
 * @Description: {con}
 * @Author: Jimmy
 * @Date: 2023-07-21 22:30:33
 * @LastEditTime: 2023-07-24 23:05:17
 * @LastEditors:  
 */
import path from 'path'
import Koa from 'koa'
import koaBody from 'koa-body';
import dotenv from 'dotenv'
import KoaStatic from 'koa-static'

import router from './router';
import { Server } from 'http';
import { errorHandler } from './utils/data.handler';

dotenv.config();
const app = new Koa();

app.use(koaBody({
    multipart: true,//开启 form-data传数据
    formidable: {
        maxFieldsSize: 200 * 1024 * 1024 //限制
    }
}))
    .use(KoaStatic(path.join(__dirname, '../statics')))
    .use(router.routes())
    .use(router.allowedMethods())

//统一的错误处理
app.on('error', errorHandler);

const run = (port: any): Server => {
    console.log(`服务已启动: http://localhost:${port}`)
    return app.listen(port)
}
export default run
